package cn.linjianhui.article.dao.provider;

import org.apache.ibatis.annotations.Param;
import org.springframework.util.StringUtils;

public class ContextMapperProvider {
    // left(regexp_replace(context, '<.+?>', ''), 50) as context
    private String column = "id,context,release_time,is_show,create_by,create_time,update_by,update_time";

    public String queryByKey(@Param("type") int type, @Param("keyWord") String keyWord) {
        String all = "select * from article_context WHERE now() > release_time and is_show = 1 order by update_time desc";
        if(StringUtils.hasText(keyWord)) {
            switch (type) {
                case 1:
                    return queryByLabel(keyWord);
                case 2:
                    return queryByTitle(keyWord);
                case 3:
                    return queryByContext(keyWord);
                default:
                    return all;
            }
        }
        return all;
    }

    public String queryByContext(@Param("keyWord") String keyWord) {
        String sql = "select * from article_context WHERE now() > release_time " +
                "and is_show = 1 and context like concat('%',#{keyWord},'%') order by update_time desc";;
        return sql;
    }

    public String queryByTitle(@Param("keyWord") String keyWord) {
        String sql = "select * from article_context WHERE now() > release_time " +
                "and is_show = 1 and title like concat('%',#{keyWord},'%') order by update_time desc";;
        return sql;
    }

    public String queryByLabel(@Param("keyWord") String keyWord) {
        String sql = "select * from article_context ac where EXISTS(" +
                "select lc.context_id from article_label_context lc " +
                    "LEFT JOIN article_label l ON lc.label_id = l.id " +
                        "WHERE l.is_enable = 1 AND l.label LIKE concat('%',#{keyWord},'%') AND lc.context_id = ac.id) " +
                "and now() > release_time and is_show = 1 order by update_time desc";
        return sql;
    }
}
